import wx
from core.config import Config
from core.tools import keyMux
import keyboard

class RecordedHotkey:
    keys_recorded = set()
    keys_pressing = set()
    recording = False
    finishOneTime = False
    final_key = None
    confirm = False

class RecordWindow(wx.Dialog):
    # 定义组件ID常量
    ID_CONFIRM_BTN = wx.NewIdRef()
    ID_STATUS_TEXT = wx.NewIdRef()
    ID_KEY_TEXT = wx.NewIdRef()
    
    def __init__(self):
        wx.Dialog.__init__(self, None, title="录制热键 - my_window_tool", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
        self.SetIcon(wx.Icon(wx.Image(Config.icon).ConvertToBitmap()))
        
        self.init_UI()

        self.SetSize((500, 400))

        self.Center()
        try:
            Config.HotkeyListener.stop()
        except:
            pass
        RecordedHotkey.recording = True
        RecordedHotkey.keys_recorded = set()
        RecordedHotkey.keys_pressing = set()
        keyboard.hook(self.onKeyEvent, suppress=True)

    def init_UI(self):
        # 创建面板
        panel = wx.Panel(self)

        # 创建布局管理器
        vbox = wx.BoxSizer(wx.VERTICAL)

        # 创建一个静态文本，居中对齐
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        status_text = wx.StaticText(panel, self.ID_STATUS_TEXT, label="正在录制热键", style=wx.ALIGN_CENTER)
        status_text.SetFont(wx.Font(16, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        status_text.SetSize(status_text.GetBestSize())
        hbox1.Add(status_text, proportion=1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=20)
        vbox.Add(hbox1, proportion=1, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=20)

        # 创建一个静态文本，居中对齐，字号变大，加粗
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        key_text = wx.StaticText(panel, self.ID_KEY_TEXT, style=wx.ALIGN_CENTER, label="请按下热键")
        key_text.SetFont(wx.Font(22, wx.FONTFAMILY_SCRIPT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))
        key_text.SetSize(key_text.GetBestSize())
        hbox2.Add(key_text, proportion=1, flag=wx.LEFT|wx.RIGHT, border=20)
        vbox.Add(hbox2, proportion=1, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=20)

        # 创建一个按钮，居中对齐
        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        confirm_btn = wx.Button(panel, self.ID_CONFIRM_BTN, label="确定")
        hbox3.Add(confirm_btn, proportion=1, flag=wx.LEFT|wx.RIGHT, border=20)
        vbox.Add(hbox3, proportion=1, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=20)
        
        # 使用ID绑定事件
        self.Bind(wx.EVT_BUTTON, self.Confirm, id=self.ID_CONFIRM_BTN)
        self.Bind(wx.EVT_CLOSE, self.onClose)

        panel.SetSizer(vbox)
        
    def onKeyEvent(self, event):
        key = keyMux(event)
        if event.event_type == 'down':
            if len(RecordedHotkey.keys_pressing) == 0:
                RecordedHotkey.keys_recorded.clear()
            RecordedHotkey.keys_pressing.add(key)
            RecordedHotkey.keys_recorded.add(key)
        elif event.event_type == 'up':
            RecordedHotkey.keys_pressing.discard(key)
            if len(RecordedHotkey.keys_pressing) <= 2:
                self.stopRecording()
            else:
                RecordedHotkey.finishOneTime = False
                
        # 使用FindWindowById获取组件
        text = self.FindWindowById(self.ID_KEY_TEXT)
        text.SetLabel("+".join(RecordedHotkey.keys_recorded))
        text.SetExtraStyle(wx.ALIGN_CENTER)

    def stopRecording(self):
        RecordedHotkey.finishOneTime = True
        RecordedHotkey.final_key = "+".join(RecordedHotkey.keys_recorded)
        
    def Confirm(self, event):
        self.stopRecording()
        RecordedHotkey.recording = False
        keyboard.unhook_all()
        self.Destroy()
        RecordedHotkey.confirm = True

    def onClose(self, event):
        self.stopRecording()
        RecordedHotkey.recording = False
        keyboard.unhook_all()
        self.Destroy()
        RecordedHotkey.confirm = False